package com.my.swas.photo.utils;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import com.baidu.aip.util.ImageUtil;
import com.my.swas.photo.config.YamlConfigUtil;
import com.my.swas.photo.controller.FileSizeEnum;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.json.JSONObject;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @Description:
 * @author: :MaYong
 */
@Slf4j
public class GoogleImageUtil {


    public static String tempFile(File file,String tempdir){
        return tempdir+File.separator+FileUtil.mainName(file)+".png";
    }

    /**
     * 图片压缩
     *      * size(width,height) 若图片横比200小，高比300小，不变
     *      * 若图片横比200小，高比300大，高缩小到300，图片比例不变 若图片横比200大，高比300小，横缩小到200，图片比例不变
     *      * 若图片横比200大，高比300大，图片按比例缩小，横为200或高为300
     *    
     */
    public static String toBig(File file,BigDecimal scale,String tempdir) {
        try {
            log.info("无损放大{}倍",scale);
            BufferedImage read = ImgUtil.read(file);
            int _w = new BigDecimal(read.getWidth()).multiply(scale).intValue();
            int _h = new BigDecimal(read.getHeight()).multiply(scale).intValue();
            String path = tempFile(file,tempdir);
            File temp = new File(path);


            Thumbnails.of(file)
                    .scale(scale.doubleValue())
                    //.size(_w,_h)
                    //.keepAspectRatio(false)
                    .toFile(temp);
            return path;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 压缩到固定大小以下
     * @param file
     * @param size MB
     * @return
     */
    public static File compression(File file,float size,String tempdir){
        log.info("文件开始压缩... ...");
        log.info("文件大小:{}",getSize(file));
        while (getSize(file) > size){
            file = compression(file,tempdir);
        }
        log.info("文件压缩完毕...大小:{}MB ...",getSize(file));
        return file;
    }

    public static float getSize(File file){
        DecimalFormat df=new DecimalFormat("0.00");//设置保留位数
        String number = df.format((float)file.length()/(1024*1024));
        return Float.valueOf(number);
    }

    public static File compression(File file,String tempdir) {
        try {
            String path = tempFile(file,tempdir);
            log.info("压缩文件地址：{}",path);
            File temp = new File(path);
            Thumbnails.of(file)
                    .scale(0.85)
                    .outputFormat("png")
                    .outputQuality(1f)
                    .toFile(temp);
            return temp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String cut(File file,BigDecimal nose_x,BigDecimal nose_y,String imageType,String tempdir) {
        try {
            BigDecimal photo_w = new BigDecimal(FileSizeEnum.getWidth(imageType));
            BigDecimal photo_h = new BigDecimal(FileSizeEnum.getHeight(imageType));
            BigDecimal point_x = nose_x.subtract(photo_w.divide(new BigDecimal(2)));
            BigDecimal point_y = nose_y.subtract(photo_h.divide(new BigDecimal(2)));
            String tempFilePath = tempdir+File.separator+FileUtil.mainName(file)+"."+FileUtil.extName(file);
            Thumbnails.of(file)
                    .sourceRegion(point_x.intValue(), point_y.intValue(), photo_w.intValue(), photo_h.intValue())
                    .size(photo_w.intValue(), photo_h.intValue())
                    //.keepAspectRatio(false)
                    .outputFormat("png")
                    .toFile(tempFilePath);
            return tempFilePath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }




}
